#include "messagedialog.h"
#include "ui_messagedialog.h"
#include <QGraphicsDropShadowEffect>
#include <QPainter>

MessageDialog::MessageDialog(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MessageDialog)
{
    ui->setupUi(this);
    ui->pushButton->installEventFilter(this);

    this->setAttribute(Qt::WA_ShowModal);
    this->setWindowModality(Qt::ApplicationModal);
    this->setWindowFlags(Qt::FramelessWindowHint);

    ui->widget->installEventFilter(this);
    ui->widget->setAttribute(Qt::WA_TranslucentBackground, true);
    this->setAttribute(Qt::WA_TranslucentBackground, true);
    ui->pushButton->setFocus();
}

MessageDialog::~MessageDialog()
{
    delete ui;
}

bool MessageDialog::eventFilter(QObject *o, QEvent *e)
{
    if (ui->pushButton == o)
    {
        if (e->type() == QEvent::FocusIn)
        {
            QGraphicsDropShadowEffect *pEffect = new QGraphicsDropShadowEffect(ui->pushButton);
            pEffect->setOffset(0, 0);
            pEffect->setColor(QColor(41, 163, 255, 255));
            pEffect->setBlurRadius(30);
            ui->pushButton->setGraphicsEffect(pEffect);
            return true;
        }
        else if (e->type() == QEvent::FocusOut)
        {
            ui->pushButton->setGraphicsEffect(NULL);
            return true;
        } else if (e->type() == QEvent::MouseButtonRelease)
        {
            this->close();
            return true;
        }
    }
    else if (ui->widget == o)
    {
        if (e->type() == QEvent::Paint)
        {
            QPainter painter(ui->widget);
            QPen pen;
            pen.setColor(QColor(1, 1, 9, 128));
            painter.setPen(pen);
            painter.setBrush(QBrush(QColor(1, 1, 9, 128)));
            painter.drawRect(ui->widget->rect());
            return true;
        }
    }
    return false;
}

void MessageDialog::setTxt(QString str)
{
    ui->label->setText(str);
}

void MessageDialog::on_pushButton_pressed()
{
    this->close();
}
